package com.yugao.fintech.antelope.system.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.system.mapper.SysUserRoleMapper;
import com.yugao.fintech.antelope.system.model.entity.SysUserRole;
import com.yugao.fintech.framework.assistant.utils.CollectionUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Component
@RequiredArgsConstructor
public class SysRoleUserManager extends ServiceImpl<SysUserRoleMapper, SysUserRole> {
    private final SysUserRoleMapper sysUserRoleMapper;

    public void deleteByRoleIds(List<Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return;
        }
        sysUserRoleMapper.delete(SysUserRole.lqw().in(SysUserRole::getRoleId, roleIds));
    }

    public Set<Long> listUserIdByRoleIds(Set<Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return Collections.emptySet();
        }
        LambdaQueryWrapper<SysUserRole> lqw = SysUserRole.lqw().in(SysUserRole::getRoleId, roleIds);
        return this.list(lqw).stream().map(SysUserRole::getUserId).collect(Collectors.toSet());
    }
}
